package com.hqyj.ctxp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.ctxp.mapper.CategoryMapper;
import com.hqyj.ctxp.pojo.Category;
import com.hqyj.ctxp.service.CategoryService;
import com.hqyj.ctxp.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    //展示商品种类信息
    @Override
    public ResultInfo selectCategoryAll(Category category) {
        Page<Category> page = new Page<>(category.getCurrentPage(), category.getPageSize());
        Page<Category> categoryPage = categoryMapper.selectCategoryAll(category.getCategoryName(), page);
        return new ResultInfo(200,"种类查询成功",categoryPage);
    }

    // 查询所有商品种类
    @Override
    public ResultInfo selectCategoryAll() {
        List<Category> categories = categoryMapper.selectList(new QueryWrapper<Category>());
        if (categories != null) {
            return new ResultInfo(200, "select success", categories);
        }
        return new ResultInfo(200, "select fail", null);
    }

    //删除种类信息
    @Override
    public ResultInfo deleteCategoryById(Integer categoryId) {
        int num = categoryMapper.deleteCategoryById(categoryId);
        if(num>0){
            return new ResultInfo(200,"删除成功");
        }
        return new ResultInfo(500,"删除失败");
    }

    //修改种类信息
    @Override
    @Transactional
    public ResultInfo updateCategory(Category category) {
        Category name = categoryMapper.selectOne(new QueryWrapper<Category>()
                .eq("category_name", category.getCategoryName()));
        if(name==null){
        UpdateWrapper<Category> wrapper = new UpdateWrapper<Category>()
                .eq("category_id", category.getCategoryId())
                .set("category_name", category.getCategoryName());
        int num = categoryMapper.update(null, wrapper);
        if(num>0){
            return new ResultInfo(200,"修改成功");
        }
        return new ResultInfo(500,"修改失败");
        }
        return new ResultInfo(500,"该种类名称已存在，请重新输入");
    }

    //新增种类信息
    @Override
    @Transactional
    public ResultInfo insertCategory(Category category) {
        Category name = categoryMapper.selectOne(new QueryWrapper<Category>()
                .eq("category_name", category.getCategoryName()));
        if(name==null){
        int num = categoryMapper.insert(category);
        if(num>0){
            return new ResultInfo(200,"增加成功");
        }
        return new ResultInfo(500,"增加失败");
        }
        return new ResultInfo(500,"该种类名称已存在，请重新输入");
    }

    //查单个
    @Override
    public ResultInfo selectOne(Integer categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        return new ResultInfo(200,"查询成功",category);
    }


}
